package com.ds.infrastructure.hyperspace.console.config.shiro;

import org.apache.shiro.authc.AuthenticationToken;

import java.io.Serializable;

/**
 * @author: yaozhendong
 * @create: 2019-11-18 10:24
 **/

public class ShiroToken implements AuthenticationToken {

    private String requestURI;
    private String httpMethod;
    /**
     * Token=sessionId:sid
     */
    private String token;
    private User currentUser;

    public ShiroToken() {

    }

    public ShiroToken(String token) {
        this.token = token;
    }

    public ShiroToken(String token, String httpMethod, String requestURI) {
        this.token = token;
        this.httpMethod = httpMethod;
        this.requestURI = requestURI;
    }

    /**
     * 身份
     *
     * @return
     */
    @Override
    public Object getPrincipal() {
        return currentUser;
    }

    /**
     * 凭据
     *
     * @return
     */
    @Override
    public Object getCredentials() {
        return token;
    }

    /**
     * 设置当前用户
     *
     * @param name
     * @param id
     */
    public void setCurrentUser(String name, String id) {
        this.currentUser = new User(name, id, token, token, httpMethod, requestURI);
    }

    /**
     * 测试账号
     *
     * @return
     */
    public User getTestUserForCloseShiro() {
        return new User("测试账号:name", "测试账号:id", "测试账号:sid", "测试账号:token", "", "");
    }

    public String getRequestURI() {
        return requestURI;
    }

    public void setRequestURI(String requestURI) {
        this.requestURI = requestURI;
    }

    public String getHttpMethod() {
        return httpMethod;
    }

    public void setHttpMethod(String httpMethod) {
        this.httpMethod = httpMethod;
    }

    /**
     * 当前用户信息
     */
    public class User implements Serializable {
        public User(String name, String id, String sid, String token, String httpMethod, String requestURI) {
            this.name = name;
            this.id = id;
            this.token = token;
            this.sid = sid;
            this.httpMethod = httpMethod;
            this.requestURI = requestURI;
        }

        private String name;
        private String id;
        private String sid;
        private String token;
        private String requestURI;
        private String httpMethod;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getToken() {
            return token;
        }

        public void setToken(String token) {
            this.token = token;
        }

        public String getSid() {
            return sid;
        }

        public void setSid(String sid) {
            this.sid = sid;
        }

        public String getRequestURI() {
            return requestURI;
        }

        public void setRequestURI(String requestURI) {
            this.requestURI = requestURI;
        }

        public String getHttpMethod() {
            return httpMethod;
        }

        public void setHttpMethod(String httpMethod) {
            this.httpMethod = httpMethod;
        }
    }
}

